<?php
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
error_reporting(E_ERROR);


//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["p"], "p", "");
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	if($g_cgival["p"] < 1) $g_cgival["p"]=1;
	
	cgi::both($g_cgival["topic_id"], "topic_id", "");
	
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	$g_cgival["pcount"] = 20;

	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["current_user_info"] = $user_detail_info;
		$g_show["is_logined"] = true;
	}
	else
	{
		$g_show["is_logined"] = false;
		$url = "/user/login.php?url={$_SERVER["REQUEST_URI"]}";
		header("Location:$url");
		exit;
	}
	
	
	$db_mblog_user_rt = new db_mblog_user_rt();
	$db_mblog_rt_count = new db_mblog_rt_count();
	$list = $db_mblog_user_rt->get_user_rt_list_by_uid($uid,$g_cgival["p"],$g_cgival["pcount"]);
	
	$mids = array();
	foreach($list as $key => $value)
	{
		
		if($value["mid"])
		{
			$list[$key]["realrt_count"] = $db_mblog_rt_count->get_mblog_count_by_mid($value["mid"]);
			$mids[] = $value["mid"];
		}
		else
		{
			$list[$key]["realrt_count"] =  0;
		}
		
	}
	
	$mblog_from_api = new mblog_from_api();
	$mblog_list = $mblog_from_api->api_get_mblog_by_mids(join(",",$mids));
	
	
//	$res = $mblog_from_api->api_get_mblog_counts_by_mids(join(",",$mids));
	
	foreach($list as $key => $value)
	{
		$list[$key]["mblog_rt_count"] =  $mblog_list["data"]["result"]["{$value["mid"]}"]["reposts_count"];
	}
	
	$count = $db_mblog_user_rt->get_user_rt_count_by_uid($uid,$g_cgival["p"],$g_cgival["pcount"]);
	
	$g_show["list"] = $list;
	$g_show["count"] = $count;
	
	
	$max_page = page_bar::get_page_max($g_show["pcount"], $g_cgival["pcount"], $g_cgival["p"]);
	$page_bar_style = array(
			"curt"		=> $g_cgival["p"],
			"max"		=> $max_page,
			"key"		=> "p",
	);
	
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar3($page_bar_style);
	
	
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);
	if (isset($_GET["debug"]))
	{
		print_r($g_show);
	}
	$smarty->display("user/manage/user_rt_view.html");
	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
	
}

